package com.css.apps.base.menu;

import com.css.common.PluginBus;
import com.css.common.exception.ExceptionFactory;
import com.css.core.model.IUser;
import com.css.util.Messages;
import com.css.webbase.WebBaseUtil;
import com.css.webbase.menu.IMenuProvider;
import com.css.webbase.menu.MenuItem;
import com.css.webbase.webwork.WebworkUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;

public class MenuFactory {
	
	private static Log log = LogFactory.getLog(MenuFactory.class);
	public static String menuCacheName = MenuFactory.class.getName();
	private static String sysMode = com.css.core.configuration.ConfigurationManager.getMode();

	/**
	 * 获取用户菜单
	 * @return
	 */
	public static java.util.List<MenuItem> getUserMenu() {
		java.util.List<MenuItem> result = new ArrayList<MenuItem>(0);
		IUser user = WebBaseUtil.getCurrentUser();
		if (user == null) {
			log.error(Messages.getString("systemMsg.sessionInvalid"));
			return result;
		}
		return getUserMenu(user);
	}
	@SuppressWarnings("unchecked")
	public static java.util.List<MenuItem> getUserMenu(IUser user) {
		IMenuProvider menuProvider = PluginBus.getPlugin(IMenuProvider.class,
				sysMode);
		// 菜单加载器不存在
		if (menuProvider == null)
			throw ExceptionFactory.makeBaseException(
					"systemMsg.fetchMenuFailed", new String[] { sysMode });
		java.util.List<MenuItem> result = new ArrayList<MenuItem>(0);

		if (user == null) {
			log.error(Messages.getString("systemMsg.sessionInvalid"));
			return result;
		}
		String userId = user.getUserId();
		// 暂存session中，可以存储到缓存中
		Object tMenus = getCachedMenuRootList(userId);
		if (tMenus == null) {
			try {
				tMenus = menuProvider.getAllMenu(user);
			} catch (Exception e) {
				log.error(Messages.getString("systemMsg.fetchMenuFailed")
						+ ":[" + userId + "][" + user.getRealName() + "]", e);
			}
		}
		if (tMenus != null) {
			result = (java.util.List<MenuItem>) tMenus;
			cacheMenuRootList(userId, tMenus);
		}
		return result;
	}
	public static java.util.List<MenuItem> getUserMenu(IUser user,String sysId) {
		com.css.apps.base.menu.service.IMenuProvider menuProvider = PluginBus.getPlugin(com.css.apps.base.menu.service.IMenuProvider.class, sysMode);
		// 菜单加载器不存在
		if (menuProvider == null)
			throw ExceptionFactory.makeBaseException("systemMsg.fetchMenuFailed", new String[] { sysMode });
		java.util.List<MenuItem> result = new ArrayList<MenuItem>(0);
		
		if (user == null) {
			log.error(Messages.getString("systemMsg.sessionInvalid"));
			return result;
		}
		String userId = user.getUserId();
		try {
			result = menuProvider.getAllMenu(user,sysId);
		} catch (Exception e) {
			log.error(Messages.getString("systemMsg.fetchMenuFailed")
					+ ":[" + userId + "][" + user.getRealName() + "]", e);
		}
		return result;
	}

	private static Object getCachedMenuRootList(Object userId) {
		// String key=menuCacheName+"_"+userId;
		return WebworkUtil.getActionContext().getSession().get(menuCacheName);
		// return CacheMan.getObject(menuCacheName, userId);
	}

	@SuppressWarnings("unchecked")
	private static void cacheMenuRootList(Object userId, Object tMenus) {
		// String key=menuCacheName+"_"+userId;
		WebworkUtil.getActionContext().getSession().put(menuCacheName, tMenus);
		// CacheMan.setObject(menuCacheName, userId, tMenus);
	}

	/** 从缓存中移除子菜单 **/
	public static void removeMenuRootList(String userId) {
		// String key=menuCacheName+"_"+userId;
		WebworkUtil.getActionContext().getSession().remove(menuCacheName);
		// CacheMan.remove(menuCacheName, userId);
	}

	/**
	 * 获取每个系统下子菜单列表
	 * 
	 * @param sysMenuId
	 *            系统菜单ID
	 * @return
	 */
	public static MenuItem getCurrentMenuRoot(String sysMenuId) {
		Object tMaps = getUserMenu();
		if (tMaps != null) {
			@SuppressWarnings("unchecked")
			List<MenuItem> items = (List<MenuItem>) tMaps;
			for (MenuItem menuItem : items) {
				if (menuItem.getId().equals(sysMenuId))
					return menuItem;
			}
		}
		return null;
	}

}
